#include "fastdds.h"

FastDDS_DomainParticipantFactory* FastDDS_get_instance() {
	static FastDDS_DomainParticipantFactory instance;
	instance.create_participant = FastDDS_create_participant;
	instance.delete_participant = FastDDS_delete_participant;
	instance.factory = DomainParticipantFactory::get_instance();
	return &instance;
}

CDomainParticipant* FastDDS_create_participant(CDomainParticipantFactory* factory) {
	if (factory && factory->create_participant) {
		FastDDS_DomainParticipantFactory* _this = (FastDDS_DomainParticipantFactory*)(factory);
		if (_this->factory) {
			// DomainParticipant* participant = _this->factory->create_participant(0);
			DomainParticipant* _participant = nullptr;
			FastDDS_DomainParticipant* participant = new FastDDS_DomainParticipant;
			participant->create_publisher = FastDDS_create_publisher;
			participant->participant = _participant;
			return (CDomainParticipant*)participant;
		}
	}
	return nullptr;
}

void FastDDS_delete_participant(CDomainParticipantFactory* factory, CDomainParticipant* participant){
	if (factory && factory->delete_participant) {
		FastDDS_DomainParticipantFactory* _factory = (FastDDS_DomainParticipantFactory*)factory;
		FastDDS_DomainParticipant* _participant = (FastDDS_DomainParticipant*)participant;
		if (_factory && _participant) {
			_factory->factory->delete_participant(_participant->participant);
			delete _participant;
		}
	}
}


CPublisher* FastDDS_create_publisher(CDomainParticipant* participant, CQosPolicy* qos_list, CPublisherListener* a_listener, CStatusKind* mask) {
	if(!participant) {
		return nullptr;
	}
	FastDDS_DomainParticipant* self = (FastDDS_DomainParticipant*)participant; 
	PublisherQos qos;
	PublisherListener* listener = nullptr;
	StatusMask* _mask = nullptr;
	Publisher* publisher = self->participant->create_publisher(qos, listener, *_mask);
	CPublisher* cpub = new CPublisher();
	return cpub;
}

